//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.link.auth.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PasswordUtil {

    private static final Logger logger = LogManager.getLogger(PasswordUtil.class);

    private static String getSHA256StrJava(String str) {
        String encodeStr = "";
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException var4) {
            logger.error(var4);
        }

        return encodeStr;
    }

    private static String getMD5StrJava(String str) {
        String encodeStr = "";

        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException var4) {
            logger.error(var4);
        }

        return encodeStr;
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuilder stringBuffer = new StringBuilder();
        String temp = null;

        for (byte aByte : bytes) {
            temp = Integer.toHexString(aByte & 255);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }

            stringBuffer.append(temp);
        }

        return stringBuffer.toString();
    }

    public static String generatePwd(String str) {
        if (str != null && !"".equals(str)) {
            str = "123@!LINK" + str + "CRM";
            String shaStr = getSHA256StrJava(str);
            String md5Str = getMD5StrJava(shaStr);
            return "{LINKSHA}" + md5Str;
        } else {
            return null;
        }
    }

    public static boolean isEquals(CharSequence pwd, String encodeStr) {
        if (pwd != null && encodeStr != null) {
            if (!encodeStr.startsWith("{LINKSHA}")) {
                return false;
            } else {
                pwd = generatePwd(pwd.toString());
                return pwd != null && pwd.equals(encodeStr);
            }
        } else {
            return false;
        }
    }

}
